﻿using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

namespace MyResKit
{
    [Serializable]
    public class AssetTable
    {
        public static AssetTable Instance { get; private set; }


        public List<AssetBundleInfo> AssetBundleInfos = new List<AssetBundleInfo>();

        public static void Load()
        {
            var jsonContent = File.ReadAllText(Application.streamingAssetsPath + "/AssetBundles/" +
                                               "AssetTable.json");
            Instance = JsonUtility.FromJson<AssetTable>(jsonContent);
        }

        public string GetAssetBundleName(string assetName)
        {

            foreach (var assetBundleInfo in AssetBundleInfos)
            {
                var searchedAssetInfo = assetBundleInfo.AssetInfos.Find(assetInfo => assetInfo.AssetName == assetName);

                if (searchedAssetInfo != null)
                {
                    return searchedAssetInfo.OwnerAssetBundleName;
                }
            }

            return null;
        }
    }
}